package com.shanghaionstar.login.register.presenter;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.framework.utils.ExceptionUtil;
import com.onstar.cn.mag.joinme.RegisterRequestEntity;
import com.onstar.cn.mag.joinme.RegisterResponseEntity;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.login.register.iview.IRegisterVisitorView;
import com.shanghaionstar.login.register.service.JoinMeService;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.FinalConstants;
import com.shanghaionstar.weight.CommonInputLayout;

import rx.Subscriber;
import rx.Subscription;

/**
 * Created by 7828wc on 16/7/4.
 */
public class RegisterVisitorPresenter extends BasePresenter<RegisterResponseEntity> {
    private IRegisterVisitorView view;
    private JoinMeService service;
    private Subscription otherSubscription;

    public RegisterVisitorPresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView<RegisterResponseEntity> view) {
        super.setView(view);
        this.view = (IRegisterVisitorView) view;
        service = new JoinMeService(view.onActivity());
        setServiceProvider(service);
    }

    public void registerVisitor(final RegisterRequestEntity registerRequestEntity, final String operationType) {
        service.registerVisitor(registerRequestEntity, operationType);
    }

    public void checkUnique(final RegisterRequestEntity registerRequestEntity, final CommonInputLayout inputLayout) {
        otherSubscription = service.registerVisitorCheckUnique(registerRequestEntity).subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread()).subscribe(new Subscriber<RegisterResponseEntity>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onStart() {
                        super.onStart();
                        view.verifyResult(inputLayout, FinalConstants.Register.SHOW_LOADING);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof OnStarException) {
                            view.onResponseError((OnStarException) e);
                        } else {
                            view.onResponseError(ExceptionUtil.defaultException(new Exception(e.getMessage())));
                        }
                        view.verifyResult(inputLayout, FinalConstants.Register.CHECK_CANCEl);
                    }

                    @Override
                    public void onNext(RegisterResponseEntity registerResponseEntity) {
                        if (!CommonUtil.isNull(inputLayout.getText())) {
                            if ((!CommonUtil.isNull(registerResponseEntity.getIsEmailUnique()) && registerResponseEntity.getIsEmailUnique().equalsIgnoreCase("false")) ||
                                    (!CommonUtil.isNull(registerResponseEntity.getIsMobileUnique()) && registerResponseEntity.getIsMobileUnique().equalsIgnoreCase("false"))) {
                                view.verifyResult(inputLayout, R.string.useremail_exist);
                                view.verifyResult(inputLayout, FinalConstants.Register.CHECK_CANCEl);
                            } else {
                                if ((!CommonUtil.isNull(registerResponseEntity.getIsMobileUnique())) || (!CommonUtil.isNull(registerResponseEntity.getIsEmailUnique()))) {
                                    view.verifyResult(inputLayout, FinalConstants.Register.CHECK_OK);
                                }
                            }
                            if (!CommonUtil.isNull(registerResponseEntity.getIsUserNameUnique()) && registerResponseEntity.getIsUserNameUnique().equalsIgnoreCase("false")) {
                                view.verifyResult(inputLayout, R.string.username_exist);
                                view.verifyResult(inputLayout, FinalConstants.Register.CHECK_CANCEl);
                            } else {
                                if (!CommonUtil.isNull(registerResponseEntity.getIsUserNameUnique())) {
                                    view.verifyResult(inputLayout, FinalConstants.Register.CHECK_OK);
                                }
                            }
                        }
                    }
                });
    }

    public boolean verifyUserName(CommonInputLayout inputLayout, boolean isUniqueCheck) {
        inputLayout.setEnabled(false);
        if (checkIsNull(inputLayout)) return false;
        if (!CommonUtil.checkUsername(inputLayout.getText())) {
            view.verifyResult(inputLayout, R.string.username_error);
            return false;
        }
        view.verifyResult(inputLayout, FinalConstants.Register.NO_ERROR);
        RegisterRequestEntity registerRequestEntity = new RegisterRequestEntity();
        registerRequestEntity.setUserName(inputLayout.getText());
        if (isUniqueCheck) checkUnique(registerRequestEntity, inputLayout);
        return true;
    }

    public boolean verifyUserPass(CommonInputLayout inputLayout, String userName) {
        inputLayout.setEnabled(false);
        if (checkIsNull(inputLayout)) return false;
        if (!CommonUtil.checkInput(inputLayout.getText())) {
            view.verifyResult(inputLayout, R.string.SB021_MSG031);
            return false;
        }
        if (!CommonUtil.passwordValidate(inputLayout.getText())) {
            view.verifyResult(inputLayout, R.string.userpass_error);
            return false;
        }
        if (!CommonUtil.isNull(userName) && inputLayout.getText().equals(userName)) {
            view.verifyResult(inputLayout, R.string.password_notequals_username);
            return false;
        }
        view.verifyResult(inputLayout, FinalConstants.Register.NO_ERROR);
        return true;
    }

    public boolean verifyCode(CommonInputLayout inputLayout) {
        inputLayout.setEnabled(false);
        if (checkIsNull(inputLayout)) return false;
        view.verifyResult(inputLayout, FinalConstants.Register.NO_ERROR);
        return true;
    }

    public boolean verifyEmailOrPhone(CommonInputLayout inputLayout, boolean isUniqueCheck) {
        inputLayout.setEnabled(false);
        if (checkIsNull(inputLayout)) return false;
        if (!CommonUtil.isEmailOrPhone(inputLayout.getText())) {
            view.verifyResult(inputLayout, R.string.useremail_phone_error);
            return false;
        }
        view.verifyResult(inputLayout, FinalConstants.Register.NO_ERROR);
        RegisterRequestEntity registerRequestEntity = new RegisterRequestEntity();
        if (CommonUtil.isEmail(inputLayout.getText())) {
            registerRequestEntity.setEmailAddress(inputLayout.getText());
        } else {
            registerRequestEntity.setMobilePhoneNumber(inputLayout.getText());
        }
        if (isUniqueCheck) checkUnique(registerRequestEntity, inputLayout);
        return true;
    }


    private boolean checkIsNull(CommonInputLayout inputLayout) {
        if (CommonUtil.isNull(inputLayout.getText())) {
            view.verifyResult(inputLayout, R.string.input_user_info_not_null);
            return true;
        }
        return false;
    }

    public void sendReport(FunctionNameFindId functionName) {
        BusinessUtil.sendReportByClickType(view.onActivity(), functionName.getStringValue());
    }

    @Override
    public void finish() {
        super.finish();
        if (otherSubscription != null && !otherSubscription.isUnsubscribed())
            otherSubscription.unsubscribe();
    }
}
